using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using B.S.Sterilizationcenter.Domain.zmc;

namespace Mobileterminal.Forms.Zmc
{
    public partial class WardManagementForm : XtraForm
    {
        // 当前选中的功能
        private string currentFunction = "查看病房";

        public WardManagementForm()
        {
            InitializeComponent();
            InitializeData();
        }

        private void InitializeData()
        {
            // 初始化病房数据
            LoadWardData();
            // 初始化床位数据
            LoadBedData();
        }

        private void LoadWardData()
        {
            // 模拟病房数据
            DataTable wardTable = new DataTable();
            wardTable.Columns.Add("病房号", typeof(string));
            wardTable.Columns.Add("类型", typeof(string));
            wardTable.Columns.Add("科室", typeof(string));
            wardTable.Columns.Add("价格", typeof(decimal));

            // 添加示例数据
            wardTable.Rows.Add("W001", "单人间", "内科", 200.00m);
            wardTable.Rows.Add("W002", "双人间", "外科", 150.00m);
            wardTable.Rows.Add("W003", "多人间", "儿科", 100.00m);

            // 绑定数据到网格
            gridControlWard.DataSource = wardTable;
        }

        private void LoadBedData()
        {
            // 模拟床位数据
            DataTable bedTable = new DataTable();
            bedTable.Columns.Add("病房号", typeof(string));
            bedTable.Columns.Add("病床号", typeof(string));
            bedTable.Columns.Add("病人卡号", typeof(string));
            bedTable.Columns.Add("床位状态", typeof(string));

            // 添加示例数据
            bedTable.Rows.Add("W001", "B001", "P12345", "占用");
            bedTable.Rows.Add("W001", "B002", "", "空闲");
            bedTable.Rows.Add("W002", "B003", "P23456", "占用");
            bedTable.Rows.Add("W002", "B004", "P34567", "占用");
            bedTable.Rows.Add("W003", "B005", "", "维修");

            // 绑定数据到网格
            gridControlBed.DataSource = bedTable;
        }

        private void btnViewWard_Click(object sender, EventArgs e)
        {
            currentFunction = "查看病房";
            panelWardInfo.Visible = true;
            panelAddWard.Visible = false;
            labelTitle.Text = "病房信息查看";
        }

        private void btnAddWard_Click(object sender, EventArgs e)
        {
            currentFunction = "添加病房";
            panelWardInfo.Visible = false;
            panelAddWard.Visible = true;
            labelTitle.Text = "添加新病房";
        }

        private void btnSaveWard_Click(object sender, EventArgs e)
        {
            // 保存新病房信息的逻辑
            XtraMessageBox.Show("病房信息保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            
            // 重新加载病房数据
            LoadWardData();
            
            // 切换回查看模式
            btnViewWard_Click(sender, e);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            // 取消添加，返回查看模式
            btnViewWard_Click(sender, e);
        }

        private void gridViewWard_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            // 获取选中的病房号
            string wardNo = gridViewWard.GetRowCellValue(e.RowHandle, "病房号").ToString();
            
            // 根据病房号筛选床位数据
            FilterBedsByWard(wardNo);
        }

        private void FilterBedsByWard(string wardNo)
        {
            // 实际应用中应该从数据库筛选
            // 这里简单模拟筛选过程
            DataTable bedTable = (DataTable)gridControlBed.DataSource;
            DataView dv = bedTable.DefaultView;
            dv.RowFilter = $"病房号 = '{wardNo}'";
            gridControlBed.DataSource = dv.ToTable();
        }
    }
}